package com.itheima.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.Mapper.UserMapper;
import com.itheima.dao.pojo.User;
import com.itheima.dao.utils.SessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;

public class UserService {
//    新的分页查询 使用PageHelper插件
    public PageInfo<User> getUserFY(int currentPage){
        //分页参数
        int pageSize = 5;
        PageHelper.startPage(currentPage,pageSize);

        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用方法查询所有用户
        List<User> user = mapper.getUser();

        PageInfo<User> userPageInfo = new PageInfo<User>(user);
        session.close();
        return userPageInfo;

    }







//    -----------------------------------
    //实现搜索框查询用户
    public PageInfo<User> getUserByKeyWord(Integer currentPage ,String keyword){

        //分页参数
        int pageSize = 5;
        PageHelper.startPage(currentPage,pageSize);

        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用方法查询所有用户
        //List<User> user = mapper.getUser();
        List<User> userByKeyWord = mapper.getUserByKeyWord(keyword);

        PageInfo<User> userPageInfo = new PageInfo<User>(userByKeyWord);
        session.close();
        return userPageInfo;
//        SqlSession session = SessionFactoryUtils.getSession();
//        UserMapper mapper = session.getMapper(UserMapper.class);
//        //获取符合条件的用户集合
//        List<User> userByKeyWord = mapper.getUserByKeyWord(keyword);
//        session.close();
//        //返回用户集合
//        return userByKeyWord;


    }




//    -----------------------------------------------------
    //获取用户数据存在的个数
    public Integer getMax(){
        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        Integer max = mapper.getMax();
        session.close();
        return max;
    }


    //实现分页查询
    public ArrayList<User> pagingUser(Integer currentPage){
        //使用工具类获取会话对象
        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        Integer startIndex = (currentPage-1)*5;
        ArrayList<User> users = mapper.pagingUser(startIndex);
        session.close();
        return users;
    }



//----------------------------------------------------------------------
    //根据用户id查询用户的所有信息
    public User getUserById(Integer id) {
        //使用工具类获取会话对象
        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用方法
        User userById = mapper.getUserById(id);
        //释放资源   将查询到的用户对象返回
        session.close();
        return userById;

    }


    //    ------------------------------------------------------------------
    //修改用户的信息
    public boolean setUserById(User user) {
        //使用工具类获取会话对象
        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用方法
        Integer integer = mapper.setUserById(user);
        //判断返回值
        if (integer > 0) {
            //表示修改成功
            session.commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }


    //--------------------------------------------------------------------------
    //删除用户(修改用户所在的状态)
    public boolean deleteUser(User user) {
        //使用工具类获取会话对象
        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //获取传递过来的用户中的用户id
        Integer id = user.getId();
        //调用方法传入id对应的用户的状态
        int i = mapper.deleteUserById(id);
        //判断返回值
        if (i > 0) {
            //表示修改(删除成功)成功
            //提交事务  释放资源
            session.commit();
            session.close();
            return true;
        }
        //表示修改(删除失败)失败 返回false
        session.close();
        return false;
    }


    //----------------------------------------------------------
    //添加用户的方法
    public boolean addUser(User user) {
        //使用工具类获取会话对象
        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

        //通过email查询用户是否存在
        String email = user.getEmail();
        User userByEmail = mapper.getUserByEmail(email);

        //判断查询的返回值是为null
        if (userByEmail == null) {
            //表示查询的用户不存在可以添加
            //调用方法添加用户  返回结果
            int i = mapper.addUser(user);
            //根据返回值判断是否插入成功

            if (i > 0) {
                //返回值大于0 表示插入成功
                //提交事务
                session.commit();
                //释放资源
                session.close();
                return true;
            }
        }
        //表示用户存在或者出现错误 返回false
        session.close();
        return false;
    }


    //----------------------------------------------------------------------------------
    //查询所有用户的方法
    public List<User> getUser() {
        //使用工具类获取会话对象
        SqlSession session = SessionFactoryUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        //返回查询到的用户集合
        List<User> user = mapper.getUser();
        //释放资源
        session.close();
        //返回查询的数据
        return user;
    }
}
